package com.finance.action.android.customer;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.JsonVo;
import com.finance.entity.customer.UserInfo;
import com.finance.service.customer.UserInfoService;

@Controller("UserInfoAction")
public class UserInfoAction {

	@Resource(name = "UserInfoService")
	private UserInfoService userInfoService;
	/**
	 * 添加客户信息
	 * @param kfUserId 客户id
	 * @param userId 业务员id
	 * @param kfUserName 客户姓名
	 * @param kfUserPhone 客户电话
	 * @param kfPhonePassword 客户服务商密码
	 * @param kfUserAdds 客户地址
	 * @param kfSex 性别（1男 2女）
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
	@RequestMapping("adduserInfo")
	public JsonVo<UserInfo> adduserInfo(String kfUserId, String userId,
			String kfUserName, String kfUserPhone, String kfPhonePassword,
			String kfUserAdds,Integer kfSex,HttpServletRequest request) throws UnsupportedEncodingException {
		JsonVo<UserInfo> json = new JsonVo<UserInfo>();
		UserInfo userInfo = new UserInfo();
		if (!kfUserId.equals("")) {
			String kfUserId1 = request.getParameter("kfUserId");
		    String kfUserId2=new String(kfUserId1.getBytes("ISO-8859-1"),"UTF-8");
			userInfo.setKfUserId(kfUserId2);
		}
		UserInfo userInfo2 = userInfoService.selectByPrimaryKey(userInfo.getKfUserId());
		if (userInfo2 != null) {
			json.setCode(1);
			json.setMsg("当前身份证客户已存在，提交失败");
			json.setSuccess(false);
			return json;
		}
		if (!userId.equals("")) {
			String userId1 = request.getParameter("userId");
		    String userId2=new String(userId1.getBytes("ISO-8859-1"),"UTF-8");
			userInfo.setUserId(userId2);
		}
		if (!kfUserName.equals("")) {
			String kfUserName1 = request.getParameter("kfUserName");
		    String kfUserName2=new String(kfUserName1.getBytes("ISO-8859-1"),"UTF-8");
			userInfo.setKfUserName(kfUserName2);
		}
		if (!kfUserPhone.equals("")) {
			String kfUserPhone1 = request.getParameter("kfUserPhone");
		    String kfUserPhone2=new String(kfUserPhone1.getBytes("ISO-8859-1"),"UTF-8");
			userInfo.setKfUserPhone(kfUserPhone2);
		}
		if (!kfPhonePassword.equals("")) {
			String kfPhonePassword1 = request.getParameter("kfPhonePassword");
		    String kfPhonePassword2=new String(kfPhonePassword1.getBytes("ISO-8859-1"),"UTF-8");
			userInfo.setKfPhonePassword(kfPhonePassword2);
		}
		if (!kfUserAdds.equals("")) {
			String kfUserAdds1 = request.getParameter("kfUserAdds");
		    String kfUserAdds2=new String(kfUserAdds1.getBytes("ISO-8859-1"),"UTF-8");
			userInfo.setKfUserAdds(kfUserAdds2);
		}
		userInfo.setKfSex(kfSex);
		userInfo.setKfCreationTime(new Date());
		userInfoService.insert(userInfo);
		json.setCode(200);
		json.setContent(userInfo);
		json.setMsg("成功");
		json.setSuccess(true);
		return json;
	}
	
	@ResponseBody
	@RequestMapping("findUserinfo")
	public JsonVo<List<UserInfo>> findUserInfo(String userId){
		JsonVo<List<UserInfo>> json = new JsonVo<List<UserInfo>>();
		List<UserInfo> list = userInfoService.findByListUserId(userId);
		if (list.size() == 0) {
			json.setCode(1);
			json.setMsg("当前业务员下面没有数据");
			json.setSuccess(false);
		}else {
			json.setContent(list);
			json.setCode(200);
			json.setMsg("成功");
			json.setSuccess(true);
		}
		return json;
	}
}
